from src.conf.base_conf import BaseConf


class Backup(BaseConf):
    __week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

    def __init__(self, path: str):
        """备份配置文件"""
        super().__init__(path)

    def _reload_from_dict(self, conf_dict: dict):
        self.on = conf_dict.get("自动备份", False)
        self.bds_hot_backup = conf_dict.get("bds热备份", False)
        # 循环备份日与时间处理后使用
        self.__bak_day = conf_dict.get("周备份日", [1, 2, 3, 4, 5, 6, 7])
        self.re_bak_day = [self.__week[i - 1] for i in self.__bak_day]
        self.__bak_time = conf_dict.get("备份时间", ["02:01", "23:59"])
        self.re_bak_time = [str(t).replace(":", "").replace("：", "") + "00"
                            for t in self.__bak_time]
        self.world_dirs = conf_dict.get("存档路径", ["worlds"])
        self.bak_dir = conf_dict.get("保存位置", "bak")
        self.bak_name = conf_dict.get("保存文件名", "%Y-%m-%d-%H-%M")
        self.bak_limit_num = conf_dict.get("备份保留数量", -1)
        self.clear_bak_first = conf_dict.get("先清理后备份", False)
        self.compress_format = conf_dict.get("联动压缩格式", "zip")

    def _check(self):
        # 存档路径自动转换
        if type(self.world_dirs) is str:
            self.world_dirs = [self.world_dirs]

    def __str__(self) -> str:
        conf_dict = {
            "自动备份": self.on,
            "bds热备份": self.bds_hot_backup,
            "周备份日": self.__bak_day,
            "备份时间": self.__bak_time,
            "存档路径": self.world_dirs,
            "保存位置": self.bak_dir,
            "保存文件名": self.bak_name,
            "备份保留数量": self.bak_limit_num,
            "先清理后备份": self.clear_bak_first,
            "联动压缩格式": self.compress_format,
            "描述": "请不要在保存位置存放其他文件！将会被删除"
        }
        return self._dict_to_str(conf_dict)
